/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

Ext.QuickTips.init();
var _pageSize = 20;
var _pageSizeSmall = 10;
var fieldName = ['id', 'name', 'day1', 'day2', 'day3', 'day4', 'day5', 'day6', 'day7', 'day8'];
var fieldLabel = ['ID', '名称', ];
var fieldType = [];
var loadServlet = basePath + 'servlet/data/RTimeCardLoad';
var paramsSetServlet = basePath + 'servlet/point/ParamsSet';
var paramsRecallServlet = basePath + 'servlet/point/ParamsRecall';
var theme = '补采';
var _title = currentOrgName + theme + '管理';
var now = new Date();
function buildWindow() {
    var yearFrom = now.getFullYear();
    var monthFrom = now.getMonth();
    var yearsStore = new Ext.data.SimpleStore({
        fields: ['year'],
        data: [[yearFrom], [yearFrom - 1]]
    });
    var monthsStore = new Ext.data.SimpleStore({
        fields: ['month'],
        data: [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]]
    });
    var store = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: fieldName
                , baseParams: {yearFrom: yearFrom, monthFrom: monthFrom, start: 0, limit: _pageSize}
        , autoLoad: true
    });
    function renderer(v, metaData, record, rowIndex, colIndex, store) {
        var value = parseInt(v);
        var imageName;
        if (value > 0) {
            imageName = 'accept.png';
        } else if (value === 0) {
            imageName = 'cancel.png';
        } else if (value === -10000) {
            imageName = 'lock.png';
        } else {
            imageName = 'lock_delete.png';
        }
        return "<image src='" + basePath + "image/icon/" + imageName + "'/>"
    }

    var checkboxSel = new Ext.grid.CheckboxSelectionModel();
    var columns = [];
    columns.push(checkboxSel);
    columns.push({header: fieldLabel[0], sortable: true, dataIndex: fieldName[0], hidden: true});
    columns.push({header: '监测点名称', sortable: true, dataIndex: 'name'});
    for (var i = 1; i <= 31; i++) {
        columns.push({header: i, dataIndex: 'day' + i, width: 30, renderer: renderer});
    }

    function search() {
        store.reload();
    }

    var grid = new Ext.grid.GridPanel({
        title: _title,
        store: store,
        //selModel:Ext.grid.CellSelectionModel,
        //sm: checkboxSel,
        sm: new Ext.grid.CellSelectionModel({
            listeners: {
                cellselect: function(sm, row, col) {

                    alert("数据：");
                }
            }
        }),
        //autoExpandColumn: fieldLabel[12],
        columns: columns,
        //autoExpandColumn: fieldName[2],
        //renderTo: Ext.getBody(),
        //width: 780,
        //height: 590,
        /*columns:[
         checkboxSel
         ,{header: fieldLabel[0], sortable: true, dataIndex: fieldName[0], hidden: true}
         ,{header: 1, dataIndex: 'day1', width: 30,renderer:renderer}
         
         ],*/
        loadMask: true,
        tbar: new Ext.Toolbar([{
                text: "选择开始日期:", xtype: 'label', style: {marginLeft: '20px'}
            },
            {
                xtype: 'combo',
                id: 'YearFromCombo',
                store: yearsStore,
                displayField: 'year',
                valueField: 'year',
                editable: false,
                mode: 'local',
                forceSelection: true,
                triggerAction: 'all',
                width: 80,
                selectOnFocus: true,
                style: {marginLeft: '20px'},
                value: yearFrom,
                listeners: {
                    select: function(combo, record, index) {
                        yearFrom = this.getValue();
                        store.baseParams.yearFrom = this.getValue();
                    }
                }
            },
            {
                text: "年", xtype: 'label', style: {marginLeft: '20px'}
            },
            {
                xtype: 'combo',
                id: 'MonthFromCombo',
                store: monthsStore,
                displayField: 'month',
                valueField: 'month',
                editable: false,
                mode: 'local',
                forceSelection: true,
                triggerAction: 'all',
                width: 60,
                selectOnFocus: true,
                style: {marginLeft: '20px'},
                value: monthFrom,
                listeners: {
                    select: function(combo, record, index) {
                        monthFrom = this.getValue();
                        store.baseParams.monthFrom = monthFrom;
                    }
                }
            },
            {text: '查询', iconCls: 'icon-search', handler: search},
            {text: '补采', iconCls: 'icon-add', handler: ''},
            {text: '人工录入', iconCls: 'icon-edit', handler: ''},
            {text: '锁定', iconCls: 'icon-delete', handler: ''}
        ]),
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: "目前没有" + theme + '信息'
        }),
           listeners: {
                'render': function() {
                    tbarPoint.render(this.tbar);
                }
            }
    });
    //grid.el.center();
    //store.load({params: {start: 0, limit: _pageSize}});

    var applicationUI = new Ext.Viewport({
        layout: "fit",
        items: [
            grid
        ]
    });
}
Ext.onReady(buildWindow);
